package com.wy.powerbuilder.controller.ljh;

import com.github.pagehelper.PageInfo;
import com.wy.powerbuilder.entity.ljh.Device;
import com.wy.powerbuilder.entity.ljh.Repair;
import com.wy.powerbuilder.entity.lwg.houses;
import com.wy.powerbuilder.mapper.ljh.RepairMapper;
import com.wy.powerbuilder.service.impl.ljh.RepairServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RepairController {
    @Autowired
    private RepairMapper repairMapper;
    @Autowired
    private RepairServiceImpl repairService;
    @RequestMapping(value = {"/seleRepair"})
    public  String selByPage(Integer pageNum, Integer pageSize, Model model,String r_userName ){
        pageSize=3;
        PageInfo<Repair> pageInfos= repairService.selByPage(pageNum,pageSize,r_userName);
        model.addAttribute("pageInfos",pageInfos);
        if(r_userName==null){
            r_userName="";
        }
        model.addAttribute("r_userName",r_userName);
        return "repair-list";
    }
    @ResponseBody
    @RequestMapping(value = {"/addRepair"})
    public String addRepair(Repair repair,Model model){
        repairService.addRepair(repair);



            return "";

    }
    @RequestMapping(value ={"/delRepair"})
    public String  delRepair(Integer r_id){
        int row=repairService.delRepair(r_id);
        if (row>0){
            return "redirect:/seleRepair";
        }else {
            return "";
        }
    }
    @RequestMapping(value ={"/updRepair"})
    public String updRepair(Repair repair) {
        int row = repairService.updRepair(repair);
        System.out.println(repair.getR_id());
        if (row > 0) {
            return "seleRepair";
        } else {
                return "";
        }
    }
    @RequestMapping(value = {"/showRepair"})
    public String seleAll(Model model){
        List<Device> devices=repairService.showDevice();
        List<houses> houes=repairService.seleAll();
        model.addAttribute("houes",houes);
        model.addAttribute("devices",devices);
        return "repair-add";
    }
    @RequestMapping(value = {"/sum"})
    public String sum(Model model){
        int r=repairService.sum();
        model.addAttribute("r",r);
        return "chart-1";
    }
    @RequestMapping(value ={"/selectId"})
    public String selectId(Integer r_id,Model model){
        Repair repair=new Repair();
        repair=repairService.selectId(r_id);
        List<houses> houes=repairService.seleAll();
        model.addAttribute("houes",houes);
        model.addAttribute("repair",repair);
        if (repair!=null){
            return "repair-update";
        }else {
            return "";
        }
    }
}
